angular.module('UCX.Cmis')
.filter 'bytes', ()->
  (bytes, precision)->
    if (isNaN(parseFloat(bytes)) || !isFinite(bytes))
      return ''
    if bytes == 0
      return '0 KB'
    if typeof precision == 'undefined'
      precision = 1
    units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
    number = Math.floor(Math.log(bytes) / Math.log(1024))
    (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) +  ' ' + units[number]
.filter 'nl2br', ($sce)->
  span = document.createElement('span')
  (input)->
    if !input?
      return ''
    lines = input.split('\n')
    newLines = for line in lines
      span.innerText = line
      span.textContent = line
      span.innerHTML
    $sce.trustAsHtml(newLines.join('<br />'))
.filter 'folder', ()->
  (array)->
    result=[]
    if array&&array.length
      for i in array
        if i.baseTypeId=='cmis:folder'
          result.push(i)
      result
.filter 'document',(Utils)->
  (array,fileType)->
    result=[]
    if array&&array.length
      # search mode no parentFolder
      if !Utils.searchModeIsOn
        parentFolder = array[0].getParent()
      for i in array
        if !fileType
          if i.baseTypeId=='cmis:document'
            result.push(i)
        else if fileType == 'pdf'
          if i.baseTypeId=='cmis:document' && i.contentStreamMimeType == 'application/pdf'
            result.push(i)
        else if fileType == 'image'
          if i.baseTypeId=='cmis:document' && i.contentStreamMimeType.substr(0,i.contentStreamMimeType.indexOf("/")) == 'image'
            result.push(i)
        else if fileType == 'excel'
          if i.baseTypeId=='cmis:document' && (i.contentStreamMimeType == 'application/vnd.ms-excel' || i.contentStreamMimeType == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
            result.push(i)
        else if fileType == 'ppt'
          if i.baseTypeId=='cmis:document' && (i.contentStreamMimeType == 'application/vnd.ms-powerpoint' || i.contentStreamMimeType == 'application/vnd.openxmlformats-officedocument.presentationml.presentation')
            result.push(i)
        else if fileType == 'word'
          if i.baseTypeId=='cmis:document' && (i.contentStreamMimeType == 'application/msword' || i.contentStreamMimeType == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
            result.push(i)
        else
          result
      if !Utils.searchModeIsOn
        parentFolder.showDocumentList=result
      result
.filter 'LevelFilter', ()->
  status = {private: 'Private', confidential: 'Confidential', classified: 'Classified', topsecret: 'Topsecret'}
  (type)->
    status[type]
.filter 'isNotEmptyObj',()->
  (obj)->
    !angular.equals({},obj)